<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/4
 * Time: 19:54
 */

namespace app\shopping\controller;

use app\shopping\model\Commalltool;

class Collectcomm extends Commalltool{
    function __construct()
    {
        parent::__construct();
    }

    function index(){
        $action = $_GET["action"];
        $uid = $_GET["uid"];
        
        if(!$this->check->isUidExist($uid)){
            echo echoJson(0, "没有此用户！");
            exit;
        }

        
        if ($action == 'get') {
            $this->getCollectComm($uid);
        } else {
            $comm_id = $_GET["comm_id"];
            if (!empty($comm_id)) {
                if ($action == 'join') {
                    $this->joinCollectComm($uid, $comm_id);
                } else if ($action == 'cancel') {
                    $this->cancelCollectComm($uid, $comm_id);
                }
            } else {
                echo echoJson(0, "操作失败，账号为空！");
            }
        }
    }

    /**
     *  加入收藏
     * @param str $uid 账号
     */
    function joinCollectComm($uid, $comm_id) {
        $comm_id_arr = strToJsonArr($comm_id);
        if (!is_array($comm_id_arr) || count($comm_id_arr) < 1) {
            echo echoJson(0, "加入收藏失败,-1！");
            return;
        }
        $this->mysql->beginTransactionSQL(); //开始事务
        foreach ($comm_id_arr as $value) {
            $comm_id = $value["comm_id"];
            $comm_sort = $value["comm_sort"];
            if (!empty($comm_id) && !empty($comm_sort)) {
                $sql = "INSERT INTO sopp_collect (uid,comm_id,comm_sort,datetime) VALUES ('$uid','$comm_id','$comm_sort',now())"
                    . "  ON DUPLICATE KEY UPDATE  uid='$uid',comm_id='$comm_id',comm_sort='$comm_sort',datetime=now()";
                if (!mysql_query($sql)) {//只要有一个执行失败就滚回
                    $this->mysql->rollbackTransactionSQL(); //滚回事务
                    echo echoJson(0, "加入收藏失败,-2！");
                    return;
                }
            } else {
                $this->mysql->rollbackTransactionSQL(); //滚回事务
                echo echoJson(0, "加入收藏失败,-3！");
                return;
            }
        }
        $this->mysql->submitTransactionSQL(); //提交事务
        echo echoJson(1, "加入收藏成功！" . $sql);
    }

    /**
     *  取消收藏
     * @param str $uid 账号
     */
    function cancelCollectComm($uid, $comm_id) {
        $comm_id_arr = strToJsonArr($comm_id);
        if (!is_array($comm_id_arr) || count($comm_id_arr) < 1) {
            echo echoJson(0, "删除收藏失败,-1！");
            return;
        }
        foreach ($comm_id_arr as $value) {
            $comm_id = $value["comm_id"];
            $comm_sort = $value["comm_sort"];
            if (!empty($comm_id) && !empty($comm_sort)) {
                $sql = "DELETE FROM sopp_collect WHERE uid='$uid' AND comm_id='$comm_id' AND comm_sort='$comm_sort'";
                if (!mysql_query($sql)) {//只要有一个执行失败就滚回
                    $this->mysql->rollbackTransactionSQL(); //滚回事务
                    echo echoJson(0, "删除收藏失败,-2！");
                    return;
                }
            } else {
                $this->mysql->rollbackTransactionSQL(); //滚回事务
                echo echoJson(0, "删除收藏失败,-3！");
                return;
            }
        }
        $this->mysql->submitTransactionSQL(); //提交事务
        echo echoJson(1, "删除收藏成功！");
    }


    /**
     *  查询用户的收藏
     * @param str $uid 账号
     */
    function getCollectComm($uid)
    {
        $sql = "SELECT * FROM sopp_collect WHERE uid='$uid' ORDER BY datetime DESC";
        $result = $this->mysql->selectSQLAll($sql);
        $jsonArray = array();
        if (is_array($result)) {
            foreach ($result as $row) {
                $comm_sort = !empty($row['comm_sort']) ? $row['comm_sort'] : "";
                $comm_id = !empty($row['comm_id']) ? $row['comm_id'] : false;
                if (($comm_sort == 'all' || $comm_sort == 'home' || $comm_sort == 'consumption' || $comm_sort == 'integral' || $comm_sort == 'activity' || $comm_sort == 'merchant') && $comm_id) {
                    $arr = $this->getItemComm($comm_sort, $comm_id);
                    $arr['comm_sort'] = $comm_sort; //可以这样添加新的关联数组
                    $jsonArray[] = $arr;
                }
            }
            if (count($jsonArray)) {
                echo echoJson(1, $jsonArray);
            } else {
                echo echoJson(-1, "商品空空如也!");
            }
        } else {
            echo echoJson(-1, "商品空空如也!");
        }
    }
}